<%-- 
    Document   : view_pekerjaan
    Created on : Jul 20, 2012, 8:26:48 PM
    Author     : DarkTerror
--%>

<%@page import="java.util.ArrayList"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="additional.Help"%>
<%@page import="dao.PekerjaanDAOImpl"%>
<%@page import="entity.Pekerjaan"%>
<%@page import="java.util.List"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>Pekerjaan</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <link href="style/style.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript">
            <%
                if (request.getParameter("delete") != null) {
                    try {
                        Long id = Long.parseLong(request.getParameter("delete"));
                        Pekerjaan p = new PekerjaanDAOImpl(Help.getEM()).get(id);

                        if (p != null) {
                            out.write("if(confirm('Hapus Karyawan beserta Alokasinya?')) { \n"
                                    + "window.location='HapusPekerjaan?id=" + p.getId() + "'; \n"
                                    + "} else { \n"
                                    + "window.location.replace('view_pekerjaan.jsp'); \n"
                                    + "}");
                        } else {
                            out.write("alert('Pekerjaan Tidak Ditemukan'); \n");
                            out.write("window.location.replace('view_pekerjaan.jsp');");
                        }
                    } catch (Exception e) {
                        out.write("alert('Terjadi Kesalahan'); \n");
                        out.write("window.location.replace('view_pekerjaan.jsp');");
                    }
                }
            %>
        </script>
    </head>
    <body>
        <div id="central">
            <div id="menuatas">
                <marquee>
                    <h3> Welcome </h3>
                </marquee>
            </div>

            <div id="header">
                <%                    boolean isLogged = false;

                    if (request.getSession().getAttribute("user") != null) {
                        out.write("<a href='Logout'>LogOut</a>");
                        isLogged = true;
                    } else {
                        out.write("<a href='login.jsp'>LogIn</a>");
                    }
                %> 
            </div>

            <ul id="navlist">
                <li><a id="n1" href="index.jsp"><span>Home</span></a></li>
                <li><a id="n2" href="view_karyawan.jsp"><span>Karyawan</span></a></li>
                <li><a id="n3" href="view_pekerjaan.jsp"><span>Pekerjaan</span></a></li>
                <li><a id="n4" href="view_alokasi.jsp"><span>Alokasi Pekerjaan</span></a></li>                               
                <li><a id="n5" href="about.jsp"><span>About</span></a></li>                               
            </ul>
            <div id="content">                
                <div id="rightside">
                    <h1>Data Pekerjaan Karyawan</h1>
                    <form method="get" action="view_pekerjaan.jsp">
                        <p>Pekerjaan
                            <input type="text" name="txtCari" value=""> 
                            <input type="submit" value="Search">
                        </p>
                    </form>
                    <hr>
                    <table border="0" width="100%">
                        <tr>
                            <th>ID Pekerjaan</th>
                            <th>Nama Pekerjaan</th>
                            <th>Tanggal Mulai</th>
                            <th>Tanggal Selesai</th>
                        </tr>
                        <%
                            try {
                                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                                List<Pekerjaan> listPekerjaan = new ArrayList<Pekerjaan>();
                                String pekerjaan = request.getParameter("txtCari");

                                if (pekerjaan != null) {
                                    listPekerjaan = new PekerjaanDAOImpl(Help.getEM()).findPekerjaan(pekerjaan);
                                } else {
                                    listPekerjaan = new PekerjaanDAOImpl(Help.getEM()).gets();
                                }

                                for (Pekerjaan p : listPekerjaan) {
                                    out.write("<tr> \n");
                                    out.write("<td>" + p.getId() + "</td> \n");
                                    out.write("<td>" + p.getNama() + "</td> \n");
                                    out.write("<td>" + sdf.format(p.getTglMulai()) + "</td> \n");
                                    out.write("<td>" + sdf.format(p.getTglSelesai()) + "</td> \n");
                                    out.write("<td> <a href='detail_pekerjaan.jsp?id=" + p.getId() + "'>Detail</a> </td> \n");
                                    if (isLogged) {
                                        out.write("<td> <a href='view_pekerjaan.jsp?delete=" + p.getId() + "'>Hapus</a></td>");
                                        out.write("<td> <a href='edit_pekerjaan.jsp?id=" + p.getId() + "'>Edit</a></td>");
                                    }
                                    out.write("</tr> \n");
                                }
                            } catch (Exception e) {
                                out.write(e.getMessage());
                            }
                        %>
                    </table> 

                    <br>
                    <%
                        if (isLogged) {
                            out.write("<a href='add_pekerjaan.jsp'>Tambah Pekerjaan</a>");
                        }
                    %>                                      
                </div>
                <div id="footer">
                    <p>&COPY; copyright TR PERSISTENCE</p>
                </div>
            </div>
        </div>
    </body>
</html>
